package com.campus.help.utils;

import com.campus.help.service.MessageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 消息工具类
 * 提供各种场景下的消息发送功能
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class MessageUtils {
    
    private final MessageService messageService;
    
    /**
     * 发送劳务申请通知（给发布者）
     */
    public void sendLaborApplicationNotification(Long publisherId, Long laborId, String laborTitle, 
                                                 String applicantName, Long applicantId) {
        try {
            messageService.sendLaborApplicationMessage(publisherId, laborId, laborTitle, applicantName, applicantId);
            log.info("劳务申请通知发送成功: publisherId={}, laborId={}, applicantName={}", 
                    publisherId, laborId, applicantName);
        } catch (Exception e) {
            log.error("发送劳务申请通知失败: publisherId={}, laborId={}, error={}", 
                    publisherId, laborId, e.getMessage());
        }
    }
    
    /**
     * 发送劳务审核结果通知（给申请者）
     */
    public void sendLaborReviewNotification(Long applicantId, Long laborId, String laborTitle,
                                            boolean approved, String reviewNote, 
                                            Long reviewerId, String reviewerName) {
        try {
            String result = approved ? "approved" : "rejected";
            messageService.sendLaborReviewMessage(applicantId, laborId, laborTitle, result, 
                    reviewNote, reviewerId, reviewerName);
            log.info("劳务审核通知发送成功: applicantId={}, laborId={}, result={}", 
                    applicantId, laborId, result);
        } catch (Exception e) {
            log.error("发送劳务审核通知失败: applicantId={}, laborId={}, error={}", 
                    applicantId, laborId, e.getMessage());
        }
    }
    
    /**
     * 发送系统通知
     */
    public void sendSystemNotification(String title, String content, Long receiverId) {
        try {
            messageService.sendSystemMessage(title, content, receiverId);
            log.info("系统通知发送成功: title={}, receiverId={}", title, receiverId);
        } catch (Exception e) {
            log.error("发送系统通知失败: title={}, receiverId={}, error={}", 
                    title, receiverId, e.getMessage());
        }
    }
    
    /**
     * 发送欢迎消息（新用户注册时）
     */
    public void sendWelcomeMessage(Long userId, String userName) {
        String title = "欢迎加入校园帮助平台";
        String content = String.format("亲爱的%s，欢迎您加入校园帮助平台！\n\n" +
                "在这里您可以：\n" +
                "• 发布各种校园服务需求\n" +
                "• 申请感兴趣的兼职工作\n" +
                "• 与同学们互帮互助\n\n" +
                "如有任何问题，请随时联系我们。祝您使用愉快！", 
                userName != null ? userName : "同学");
        
        sendSystemNotification(title, content, userId);
    }
    
    /**
     * 发送任务完成通知
     */
    public void sendTaskCompletionNotification(Long userId, String taskTitle, String taskType) {
        String title = "任务完成通知";
        String content = String.format("恭喜您成功完成了%s任务「%s」！\n\n" +
                "您的信用积分已增加，继续保持良好的服务质量，获得更多机会！", 
                getTaskTypeName(taskType), taskTitle);
        
        sendSystemNotification(title, content, userId);
    }
    
    /**
     * 发送积分奖励通知
     */
    public void sendPointsRewardNotification(Long userId, int points, String reason) {
        String title = "积分奖励";
        String content = String.format("恭喜您获得了 %d 积分奖励！\n\n奖励原因：%s\n\n" +
                "继续努力，更多奖励等着您！", points, reason);
        
        sendSystemNotification(title, content, userId);
    }
    
    /**
     * 发送账户安全提醒
     */
    public void sendSecurityNotification(Long userId, String action, String location) {
        String title = "账户安全提醒";
        String content = String.format("您的账户在 %s 进行了%s操作。\n\n" +
                "如果这不是您本人的操作，请立即修改密码并联系客服。", 
                location != null ? location : "未知位置", action);
        
        sendSystemNotification(title, content, userId);
    }
    
    /**
     * 批量发送系统维护通知
     */
    public void sendMaintenanceNotification(String startTime, String endTime, String reason) {
        String title = "系统维护通知";
        String content = String.format("系统将于 %s 至 %s 进行维护。\n\n" +
                "维护原因：%s\n\n" +
                "维护期间可能影响正常使用，请合理安排时间。给您带来的不便敬请谅解。", 
                startTime, endTime, reason);
        
        // 发送给所有用户
        sendSystemNotification(title, content, null);
    }
    
    private String getTaskTypeName(String taskType) {
        switch (taskType) {
            case "labor":
                return "劳务";
            case "errand":
                return "跑腿";
            case "express":
                return "快递";
            case "substitute":
                return "代课";
            default:
                return "任务";
        }
    }
}
